package cn.rkylin.oms.base.refundReasonType.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;

import cn.rkylin.core.ApolloMap;
import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.base.refundReasonType.dao.IRdfundReasonTypeDAO;
import cn.rkylin.oms.base.refundReasonType.domain.EcRefundReason;
import cn.rkylin.oms.base.refundReasonType.domain.RdfundReasonRelation;
import cn.rkylin.oms.base.refundReasonType.domain.RdfundReasonType;
import cn.rkylin.oms.base.refundReasonType.vo.RdfundReasonTypeVO;

/**
 * 退款原因类型服务实现
 * 
 * @author qitao
 * @version 1.0
 * @created 17-7月-2017
 */
@Service
public class RdfundReasonTypeService extends ApolloService {

    @Autowired
    private IRdfundReasonTypeDAO rdfundReasonTypeDAO;

    /**
     * 查询退款原因类型
     * 
     * @param page 页号
     * @param rows 页大小
     * @param RdfundReasonTypeVO 退款原因类型VO
     */
    public PageInfo<RdfundReasonTypeVO> findByWhere(int page, int rows, RdfundReasonTypeVO rdfundReasonTypeVO) throws Exception {
        PageInfo<RdfundReasonTypeVO> rdfundReasonTypeVOList = findPage(page, rows, "pageSelectRdfundReasonType", rdfundReasonTypeVO);
        return rdfundReasonTypeVOList;
    }

    /**
     * 查询退款原因类型根据退款原因编码
     * 
     * @param params 数据参数
     */
    public int getRdfundReasonTypeCountByCode(Map<String, Object> params) throws Exception {
        return rdfundReasonTypeDAO.getRdfundReasonTypeCountByCode(params);
    }

    /**
     * 插入退款原因类型
     * 
     * @param params 数据参数
     */
    public int insertRdfundReasonType(ApolloMap<String, Object> params) throws Exception {
        return rdfundReasonTypeDAO.insertRdfundReasonType(params);
    }

    /**
     * 查询退款原因类型
     * 
     * @param params 数据参数
     */
    public RdfundReasonType getRdfundReasonTypeInfo(Map<String, Object> params) throws Exception {
        return rdfundReasonTypeDAO.getRdfundReasonTypeInfo(params);
    }
    
    /**
     * 查询退款原因类型
     * 
     * @param params 数据参数
     */
    public RdfundReasonType getRdfundReasonTypeInfoWithEcRefundReason(String ecRefundReason) throws Exception {
        return rdfundReasonTypeDAO.getRefundReasonTypeInfo(ecRefundReason);
    }

    /**
     * 修改退款原因类型
     * 
     * @param params 数据参数
     */
    public int updateRdfundReasonType(ApolloMap<String, Object> params) throws Exception {
        return rdfundReasonTypeDAO.updateRdfundReasonType(params);
    }

    /**
     * 删除退款原因类型
     * 
     * @param params 数据参数
     */
    public int deleteRdfundReasonType(ApolloMap<String, Object> params) throws Exception {
        rdfundReasonTypeDAO.deleteRdfundReasonRelationForType(params);

        return rdfundReasonTypeDAO.deleteRdfundReasonType(params);
    }

    /**
     * 查询平台退款原因关联list
     * 
     * @param params 数据参数
     */
    public List<RdfundReasonRelation> getRdfundReasonRelationList(Map<String, Object> params) throws Exception {
        return rdfundReasonTypeDAO.getRdfundReasonRelationList(params);
    }

    /**
     * 删除平台退款原因关联
     * 
     * @param params 数据参数
     */
    public int deleteRdfundReasonRelation(ApolloMap<String, Object> params) throws Exception {
        return rdfundReasonTypeDAO.deleteRdfundReasonRelation(params);
    }

    /**
     * 查询平台退款原因list
     * 
     * @param params 数据参数
     */
    public List<EcRefundReason> getEcRefundReasonList(Map<String, Object> params) throws Exception {
        return rdfundReasonTypeDAO.getEcRefundReasonList(params);
    }

    /**
     * 保存平台退款原因关联
     * 
     * @param params 数据参数
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void saveRefundReasonRelation(List dataList) throws Exception {
        for (int i = 0; i < dataList.size(); i++) {
            ApolloMap<String, Object> params = (ApolloMap<String, Object>) dataList.get(i);
            int count = rdfundReasonTypeDAO.getRefundReasonRelationCountByCondition(params);

            if (count > 0) {
                throw new Exception("chcekErr|平台退款原因名称:" + params.get("ecRefundReason") + ", 货物状态:"
                        + (params.get("goodsStatus") == "received" ? "已收到货" : "未收到货") + ", 买家是否退货:"
                        + (params.get("goodsReturn") == "y" ? "退货" : "不退货") + " 已经匹配过退款原因类型");
            }

            rdfundReasonTypeDAO.insertRdfundReasonRelation(params);
        }
    }

}
